package com.ssm.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.entity.system.Role;
import com.ssm.service.system.RoleService;
import com.ssm.web.Page;

@Controller
@RequestMapping("system/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;

	@RequestMapping("list")
	public ModelAndView list(ModelAndView mav, Role role) {
		Page<Role> page = roleService.queryList(role);
		mav.addObject(page);
		mav.setViewName("system/role/list");
		return mav;
	}
	
	@RequestMapping("toAdd")
	public String toAdd() {
		return "system/role/add";
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Object add(Role role) {
		return roleService.save(role);
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(@RequestParam("ids[]") String[] ids) {
		return roleService.delete(ids);
	}
	
	@RequestMapping("toUpdate")
	public ModelAndView toUpdate(ModelAndView mav, String roleId) {
		Role role = roleService.getDetail(roleId);
		mav.addObject(role);
		mav.setViewName("system/role/update");
		return mav;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public Object update(Role role) {
		return roleService.update(role);
	}
	
	@RequestMapping("getZtree")
	@ResponseBody
	public Object ztree(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		return roleService.ztree(roleId);
	}
}
